//
//  avatar.swift
//  MasterBoy
//
//  Created by kyson on 2024/10/12.
//

import Foundation
import SnapKit
import UIKit
import YYText


class MBDAvaterCell: MBDCommonCell {
    var avaterImageView : UIImageView!
    var usernameLabel : YYLabel!

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initialize()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.initialize()
    }
    
    override var cellModel: Any! {
        didSet {
            let cellModel = self.cellModel as! MBDMineAvatarCellModel
            self.accessable = cellModel.accessory
            self.separateLine.isHidden = cellModel.hideSeparateLine ?? true
            
            let usernameAttributeString = NSMutableAttributedString(string: cellModel.title ?? "用户")
            usernameAttributeString.yy_font = UIFont.systemFont(ofSize: 16)
            usernameAttributeString.setDynamicTextColor()
            usernameLabel.attributedText = usernameAttributeString
            
            if cellModel.avatar.hasPrefix(MBDPath2URLUtils.filePathPrefix) {
                let imageName = cellModel.avatar.replacingOccurrences(of: MBDPath2URLUtils.filePathPrefix, with: "")
                self.avaterImageView.image = UIImage(named: imageName)
            } else {
                let url = URL.init(string: cellModel.avatar)
                self.avaterImageView.kf.setImage(with: url)
            }            
        }
    }

    func initialize() {
        
        self.contentView.setDynamicForgroundColor()

        avaterImageView = UIImageView()
        avaterImageView.layer.cornerRadius = 33
        
        avaterImageView.clipsToBounds = true
        avaterImageView.layer.borderWidth = 0.5
        avaterImageView.layer.borderColor = UIColor(colorHex: 0x4273d0).cgColor

        avaterImageView.image = UIImage(named: "ico_appicon")
        avaterImageView.contentMode = .scaleAspectFill
        self.contentView.addSubview(avaterImageView)
                
        usernameLabel = YYLabel()
        self.contentView.addSubview(usernameLabel)

        avaterImageView.snp.makeConstraints { make in
            make.left.equalTo(30)
            make.centerY.equalTo(self.contentView)
            make.size.equalTo(CGSizeMake(66, 66))
        }
        
        usernameLabel.snp.makeConstraints { make in
            make.left.equalTo(avaterImageView.snp.right).offset(15)
            make.centerY.equalTo(avaterImageView)
        }
    }
}

